package com.zyg.shop.controller;

import com.zyg.common.utils.FastDFSClient;
import com.zyg.common.utils.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class UploadController {
    @PostMapping("/shop/upload")
    public R upload(MultipartFile file) throws Exception {//必须与前端传过来formData中的key的名称一致
//        构造fastDFS对象
        FastDFSClient client = new FastDFSClient("classpath:fastdfs.conf");
        String filename = file.getOriginalFilename();
        String extName = filename.substring(filename.lastIndexOf(".") + 1);
        //文件上传
        String path = client.uploadFile(file.getBytes(), extName);//参数1 二进制内容 参数2 文件后缀名

        String url = "http://192.168.56.16:8080/"+path;
        return R.ok().put("url",url);
    }
}
